package org.zionchina.cross;

import java.net.MalformedURLException;
import org.eclipse.core.runtime.FileLocator;
import java.net.URL;
import java.util.ArrayList;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.swt.graphics.Point;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
import org.eclipse.ui.browser.IWebBrowser;
import org.eclipse.ui.internal.browser.WebBrowserView;
import org.zionchina.cross.server.Tomcat55Plugin;
import org.zionchina.cross.views.*;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {

	public ApplicationWorkbenchWindowAdvisor(
			IWorkbenchWindowConfigurer configurer) {
		super(configurer);
	}

	public ActionBarAdvisor createActionBarAdvisor(
			IActionBarConfigurer configurer) {
		return new ApplicationActionBarAdvisor(configurer);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.application.WorkbenchWindowAdvisor#postWindowOpen()
	 */
	public void postWindowOpen() {
		IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
		try {
			URL installURL = Activator.getDefault().getBundle().getEntry("/"); //$NON-NLS-1$
			URL resolvedURL = FileLocator.resolve(installURL);
			Tomcat55Plugin.startTomcat();
			String home = FileLocator.toFileURL(resolvedURL).getFile()
					+ "start/Home.html";
			System.out.println(home);
			WebBrowserView view=((WebBrowserView) configurer.getWindow().getActivePage().showView(
					View.ID, "1", IWorkbenchPage.VIEW_ACTIVATE));
					view.setURL("file:///" + home);
		} catch (PartInitException e1) {
			e1.printStackTrace();
			System.out
					.println("ApplicationWorkbenchWindowAdvisor.postWindowOpen()");
		} catch (Exception e1) {
			System.out
					.println("ApplicationWorkbenchWindowAdvisor.postWindowOpen()");
		}

	}

	public void preWindowOpen() {
		IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
		configurer.setInitialSize(new Point(800, 600));
		configurer.setShowCoolBar(true);
		configurer.setShowStatusLine(true);
	}

}
